package com.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.MqIdempotent;
import com.example.constant.MqConstant;
import com.example.dto.RabbitMqMessageDTO;
import com.example.service.ConsumerService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * 消息监听 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2023/4/1
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {
    /**
     * 消费消息
     *
     * @param message 消息队列报文
     */
    @MqIdempotent // 保证MQ幂等注解
    @RabbitListener(queues = MqConstant.QUEUE_NAME)
    @Override
    public void linsten(String message) {
        System.out.println("接收消息 " + message);
        // 解析Json
        RabbitMqMessageDTO rabbitMqMessageDTO = JSONObject.parseObject(message, RabbitMqMessageDTO.class);
        System.out.println(rabbitMqMessageDTO);
        // 获取报文json
        String jsonData = rabbitMqMessageDTO.getJsonData();
        System.out.println("报文信息 " + jsonData);
    }
}
